package com.tv.nextin;

import java.util.ArrayList;

import com.tv.nextin.Utils.Common;
import com.tv.nextin.adapter.StaffAdapter;
import com.tv.nextin.bean.Staff;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

public class ListStaffActivity extends Activity implements OnTouchListener{
	
	TextView title;
	ImageButton btnBack;
	TextView txtAppoint;
	ListView listStaff;
	StaffAdapter adapter;
	
	ArrayList<Staff> staffs = new ArrayList<Staff>();
	private BroadcastReceiver mReceiver;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_list_staff);
		
		title = (TextView) findViewById(R.id.title);
		btnBack = (ImageButton) findViewById(R.id.btn_back);
		btnBack.setVisibility(View.VISIBLE);
		txtAppoint = (TextView) findViewById(R.id.txt_appoinment);
		
		setData();
		
		listStaff = (ListView) findViewById(R.id.ls_staff);
		adapter = new StaffAdapter(ListStaffActivity.this, staffs);
		listStaff.setAdapter(adapter);
		
		listStaff.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View v, int position,
					long arg3) {
				Intent t = new Intent(ListStaffActivity.this, BookingActivity.class);
				t.putExtra("staff", staffs.get(position));
				startActivity(t);
				overridePendingTransition(R.anim.trans_left_in, R.anim.over_main_left);
			}
			
		});
		
		
		init();
		receiverBroastCast();
	}
	
	ArrayList<Staff> setData(){
		staffs.add(new Staff("Kate Upton"));
		staffs.add(new Staff("Leah Dizon"));
		staffs.add(new Staff("Kate Moss"));
		return staffs;
	}
	
	void init(){
		title.setText("Select Staff");
		btnBack.setOnTouchListener(this);
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		switch (v.getId()) {
		case R.id.btn_back:
			if (event.getAction() == MotionEvent.ACTION_UP){
				closeActivity();
			}
			break;
			
		default:
			break;
		}
		return false;
	}
	
	void receiverBroastCast(){
		IntentFilter intentFilter = new IntentFilter();
		
		
		 intentFilter.addAction(Common.FINISH);
		    
		    mReceiver = new BroadcastReceiver() {
				
				@Override
				public void onReceive(Context context, final Intent intent) {
					String action = intent.getAction();
					if (action.equals(Common.FINISH)){
						finish();
					} 
				}
			};
			// register
			this.registerReceiver(mReceiver, intentFilter);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		this.unregisterReceiver(mReceiver);
	}
	void closeActivity(){
		finish();
		overridePendingTransition(R.anim.over_main_right, R.anim.trans_right_out);
	}
	
	
	@Override
	public void onBackPressed() {
		closeActivity();
	}
	
}
